package example.org.word.worddemo;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import example.org.word.wordUtils.XWPFHelper;
import example.org.word.wordUtils.XWPFHelperTable;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTNumImpl;

/**
 * @Description 导出word文档
 * @Author  Huangxiaocong
 * 2018年12月1日  下午12:12:15
 */
public class ExportWord {
    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;
    public ExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }
    /**
     * 创建好文档的基本 标题，表格  段落等部分
     * @return
     * @Author Huangxiaocong 2018年12月16日
     */
    public XWPFDocument createXWPFDocument() {
        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc);
        createTitleParagraph(doc);
        return doc;
    }


    /**
     * 创建标题样式
     * @param document
     * @Author Huangxiaocong 2018年12月16日 下午5:28:38
     */
    public void createTitleParagraph(XWPFDocument document) {
        XWPFParagraph titleParagraph = document.createParagraph();    //新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.LEFT);//样式居中

        titleParagraph.setNumILvl(BigInteger.valueOf(0));
        titleParagraph.setNumID(BigInteger.valueOf(1));
        XWPFRun titleFun = titleParagraph.createRun();    //创建文本对象
//        titleFun.setText(titleName); //设置标题的名字
        titleFun.setBold(true); //加粗
//        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(25);    //字体大小
//        titleFun.setFontFamily("");//设置字体
        //...
//        titleFun.addBreak();    //换行
    }
    /**
     * 设置表格
     * @param document
     * @param rows
     * @param cols
     * @Author Huangxiaocong 2018年12月16日
     */
    public void createTableParagraph(XWPFDocument document, int rows, int cols) {
//        xwpfHelperTable.createTable(xdoc, rowSize, cellSize, isSetColWidth, colWidths)
        XWPFTable infoTable = document.createTable(rows, cols);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "9072", STJc.CENTER);
        CTTblGrid ctTblGrid = CTTblGrid.Factory.newInstance();
        CTTblGridCol[] ctTblGridCols = new CTTblGridCol[cols];
        IntStream.range(0,cols).forEach(i->{
            CTTblGridCol c = CTTblGridCol.Factory.newInstance();
            c.setW(BigInteger.valueOf(XWPFHelperTable.defaultWidth));
            ctTblGridCols[i] = c;
        });
        ctTblGrid.setGridColArray(ctTblGridCols);
        infoTable.getCTTbl().setTblGrid(ctTblGrid);
        //合并表格
//        xwpfHelperTable.mergeCellsHorizontal(infoTable, 1, 1, 3);
        xwpfHelperTable.mergeCellsHAV(infoTable, 0, 1, 1, 2);
//        xwpfHelperTable.mergeCells(infoTable, 0, 1, 1, 2);

        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for(int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            List<XWPFTableCell> cellList = infoTableRow.getTableCells();
            for(int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(12);
                if(i % 2 != 0) {
                    cellParagraphRun.setBold(true);
                }
            }
        }
        xwpfHelperTable.setTableHeight(infoTable, 560, STVerticalJc.CENTER);
    }


    private XWPFAbstractNum createAbstractNum123() {
        CTAbstractNum ctAbstractNum = CTAbstractNum.Factory.newInstance();
        ctAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
        CTLvl ctLvl = ctAbstractNum.addNewLvl();
        ctLvl.addNewLvlText().setVal("%1.");
        ctLvl.addNewLvlJc().setVal(STJc.LEFT);
        ctLvl.addNewNumFmt().setVal(STNumberFormat.CHINESE_COUNTING);
        CTDecimalNumber ctDecimalNumber = CTDecimalNumber.Factory.newInstance();
        ctDecimalNumber.setVal(BigInteger.valueOf(1));
        ctLvl.setStart(ctDecimalNumber);
        ctLvl.addNewPPr().addNewInd().setLeft(BigInteger.valueOf(420));
//        ctLvl.addNewRPr().addNewRFonts().setHint();
        CTRPr ctrPr = ctLvl.addNewRPr();
        CTFonts ctFonts = ctrPr.addNewRFonts();
        ctFonts.setAscii("宋体");
        ctFonts.setHAnsi("宋体");
        ctFonts.setEastAsia("宋体");
        ctFonts.setCs("宋体");
        ctrPr.addNewB();
        ctrPr.addNewBCs();
        ctrPr.addNewSz().setVal(BigInteger.valueOf(32));
        ctrPr.addNewSzCs().setVal(BigInteger.valueOf(32));

        XWPFAbstractNum xwpfAbstractNum = new XWPFAbstractNum(ctAbstractNum);
        return xwpfAbstractNum;
    }


    /**
     * 往表格中填充数据
     * @param dataList
     * @param document
     * @throws IOException
     * @Author Huangxiaocong 2018年12月16日
     */
    @SuppressWarnings("unchecked")
    public void exportWord(Map<String, Object> dataList, XWPFDocument document, String savePath) throws IOException {
        XWPFNumbering xwpfNumbering= document.createNumbering();
        XWPFAbstractNum xwpfAbstractNum = createAbstractNum123();
//        xwpfAbstractNum.
        xwpfNumbering.addAbstractNum(xwpfAbstractNum);
        xwpfNumbering.addNum(BigInteger.valueOf(0));


        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleFun = paragraph.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));

        XWPFParagraph paragraph1 = document.getParagraphArray(1);
        XWPFRun titleFun1 = paragraph1.getRuns().get(0);
        titleFun1.setText(String.valueOf(dataList.get("first")));

        xwpfHelper.saveDocument(document, savePath);
    }
    /**
     * 往表格中填充数据
     * @param table
     * @param tableData
     * @Author Huangxiaocong 2018年12月16日
     */
    public void fillTableData(XWPFTable table, List<List<Object>> tableData) {
        List<XWPFTableRow> rowList = table.getRows();
        for(int i = 0; i < tableData.size(); i++) {
            List<Object> list = tableData.get(i);
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            for(int j = 0; j < list.size(); j++) {
                XWPFTableCell xwpfTableCell = cellList.get(j);
                XWPFParagraph cellParagraph = xwpfTableCell.getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                cellParagraphRun.setText(String.valueOf(list.get(j)));
            }
        }
    }
}